﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
    public class He
    {
        int _MaHe;
        public int MaHe
        {
            get { return _MaHe; }
            set { _MaHe = value; }
        }
        string _TenHe;
        public string TenHe
        {
            get { return _TenHe; }
            set { _TenHe = value; }
        }
        public He() { }

        public static He GetData(SqlDataReader reader)
        {
            He h = new He();
            h.MaHe = CType.ToInt32(reader["MaHe"]);
            h.TenHe = CType.Tostring(reader["TenHe"]);
            return h;
        }

        public static SqlConnection cnn;
        public static SqlCommand cmd;

        public static List<He> All()
        {
            List<He> list = new List<He>();
            using (cnn = MyConnection.MyConn())
            {
                cmd = cnn.CreateCommand();
                cmd.CommandText = "select * from He";
                cnn.Open();
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    He data = GetData(reader);
                    list.Add(data);
                }
                return list;
            }
        }

        public static int LayMaHe(string ten)
        {
            using (cnn = MyConnection.MyConn())
            {
                cmd = cnn.CreateCommand();
                cmd.CommandText = "select MaHe from He where TenHe = '" + ten + "'";
                cnn.Open();
                int rs = (int)cmd.ExecuteScalar();
                return rs;
            }
        }

    }
}
